﻿using DeviceAlarmService.SystemService;
using DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.SiemensS7.Models;
using System.Collections;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            ArrayList arrayList = ConfigService.GetID();

            if (arrayList is null)
            {
                return;
            }
            foreach (string item in arrayList)
            {
                if (File.Exists($"json/{item}.json"))
                {
                    string config = File.ReadAllText($"json/{item}.json");

                    DeviceAlarmService.Models.SiemensPlc.DeviceInformation d = JsonSerializer.Deserialize<DeviceAlarmService.Models.SiemensPlc.DeviceInformation>(config)!;
                    DeviceInformationModel deviceInformationModel = new DeviceInformationModel();

                    deviceInformationModel.Name = d.name;
                    deviceInformationModel.Number = d.number;
                    deviceInformationModel.Protocol = DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.Models.ProtocolEnum.SiemensS7;

                    deviceInformationModel.DeviceAddress = new()
                    {
                        CpuType = d.deviceAddress.cpuType,
                        Ip = d.deviceAddress.ip,
                        Port = d.deviceAddress.port,
                        Rack = d.deviceAddress.rack,
                        Slot = d.deviceAddress.slot,
                        IsMultiple = d.deviceAddress.isMultiple,
                    };

                    deviceInformationModel.DataAddress = new();
                    foreach (var iteml in d.dataAddress)
                    {
                        deviceInformationModel.DataAddress.Add(new()
                        {
                            AlarmAddress = iteml.alarmAddress,
                            AlarmInformation = iteml.alarmInformation,
                            IsEnable = iteml.isEnable,
                        });
                    }
                    string jsonString = JsonSerializer.Serialize(deviceInformationModel, new JsonSerializerOptions()
                    {
                        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                    });

                    File.WriteAllText($"jsons/{deviceInformationModel.Number}.json", jsonString);
                    Console.WriteLine(deviceInformationModel.Number);
                }
            }





































            //DeviceInformationModel deviceInformationModel = new DeviceInformationModel();

            //deviceInformationModel.Name = "cess";
            //deviceInformationModel.Number = "12313";
            //deviceInformationModel.protocol = DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.Models.ProtocolEnum.SiemensS7;

            //deviceInformationModel.DeviceAddress = new() { CpuType = S7.Net.CpuType.S71500, Ip = "172.16.18.101", Port = 102, Slot = 0, Rack = 1, IsMultiple = true };

            //deviceInformationModel.DataAddress = new() {
            //       new() {AlarmAddress = "M0.0",AlarmInformation="123465",IsEnable = true },
            //    new() {AlarmAddress = "M0.1",AlarmInformation="123465",IsEnable = true },
            //     new() {AlarmAddress = "M0.2",AlarmInformation="123465",IsEnable = true },
            //      new() {AlarmAddress = "M0.3",AlarmInformation="123465",IsEnable = true },
            //       new() {AlarmAddress = "M0.4",AlarmInformation="123465",IsEnable = true },
            //        new() {AlarmAddress = "M0.5",AlarmInformation="123465",IsEnable = true },
            //         new() {AlarmAddress = "M0.6",AlarmInformation="123465",IsEnable = true },
            //          new() {AlarmAddress = "M0.7",AlarmInformation="123465",IsEnable = true },
            //             new() {AlarmAddress = "M1.0",AlarmInformation="123465",IsEnable = true },
            //                     new() {AlarmAddress = "M1.1",AlarmInformation="123465",IsEnable = true },
            //     new() {AlarmAddress = "M1.2",AlarmInformation="123465",IsEnable = true },
            //      new() {AlarmAddress = "M1.3",AlarmInformation="123465",IsEnable = true },
            //       new() {AlarmAddress = "M1.4",AlarmInformation="123465",IsEnable = true },
            //        new() {AlarmAddress = "M1.5",AlarmInformation="123465",IsEnable = true },
            //         new() {AlarmAddress = "M1.6",AlarmInformation="123465",IsEnable = true },
            //          new() {AlarmAddress = "M1.7",AlarmInformation="123465",IsEnable = true },
            //};
            //string jsonString = JsonSerializer.Serialize(deviceInformationModel, new JsonSerializerOptions()
            //{
            //    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //});

            //StartUp startUp = new StartUp();
            //log log = new();
            //startUp.LogEvent += log.Log;
            //startUp.Run(jsonString);

            //while (true)
            //{
            //    Console.ReadLine();
            //    Thread.Sleep(10);
            //}

            //DeviceCommunication deviceCommunication = new(deviceInformationModel);
            //deviceCommunication.Connect();
            //deviceCommunication.Read(out var REQ);
            //deviceCommunication.Close();

        }


    }

    class log
    {
        public void Log(string s)
        {
            Console.WriteLine(s);
        }
    }
}
